import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DatePipe } from '@angular/common';
import { CustomerIdolWaterService } from './customer-idol-water.service';
import { CommonService } from 'src/app/services-common/common.service';
import { NzModalService } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-customer-idol-water',
  templateUrl: './customer-idol-water.component.html',
  styleUrls: ['./customer-idol-water.component.scss'],
  providers: [DatePipe]
})
export class CustomerIdolWaterComponent implements OnInit {

  isCollapse: boolean = false;

  seachParams: any = {
    num: '', // 综合搜索
    createTime: '',
    endTime: ''
  };

  // 发生时间
  happenTimes: any = [];

  // 收支类型options
  incomeExpenditureOptions: any = [];
  incomeExpendSelected = [];

  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  tableData: any[] = [];

  exportLoading: boolean = false;

  constructor(
    private message: NzMessageService,
    private datePipe: DatePipe,
    private services: CustomerIdolWaterService,
    private commonService: CommonService,
    private modal: NzModalService
  ) { }

  ngOnInit(): void {
    this.getIncomeExpenditure();
    this.loadTable();
  }

  // 查询账户流水交易类型
  async getIncomeExpenditure() {
    const res: any = await this.commonService.dictListData('account_transaction_type');
    if (res.code == 0) {
      this.incomeExpenditureOptions = res.data.list || [];
    } else {
      this.incomeExpenditureOptions = [];
      this.message.error(res.message);
    }
  }

  async loadTable() {
    let params = {
      ...this.seachParams,
      transactionType: this.incomeExpendSelected.join(','),
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.tableLoading = true;
    const res: any = await this.services.getIdolWaterList(params);
    this.tableParams.tableLoading = false;
    
    if (res.code == 0) {
      const { records, total } = res.data || {};
      this.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(res.message);
    }
  }

  seachData() {
    this.tableParams.page = 1;
    this.loadTable();
  }
  resetData() {
    for (let key in this.seachParams) {
      this.seachParams[key] = '';
    }
    this.happenTimes = [];
    this.incomeExpendSelected = [];

    this.tableParams.page = 1;
    this.loadTable();
  }

  timeChange(result: any) {
    this.seachParams.createTime = result.length ? this.datePipe.transform(result[0], 'yyyy-MM-dd') : '';
    this.seachParams.endTime = result.length ? this.datePipe.transform(result[1], 'yyyy-MM-dd') : '';
  }

  // 导出
  exportTable() {
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '即将导出符合本页面筛选条件的数据，为了导出速度请尽可能的控制时间范围。确认导出？',
      nzOkText: '确认',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        let params = {
          ...this.seachParams,
          transactionType: this.incomeExpendSelected.join(','),
          page: this.tableParams.page,
          pageSize: this.tableParams.pageSize
        }
        this.exportLoading = true;
        const res: any = await this.services.idolWaterExportData(params);
        this.exportLoading = false;
        
        if (res.type === 'application/octet-stream') {
          this.commonService.downLoadBlobFile(res, '爱豆流水');
        } else {
          this.message.error(res.message || '导出失败');
        }
      },
      nzCancelText: '取消'
    });
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // 页数改变时的回调函数
  onPageSizeChange(sizeNum: number) {
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  // 收支类型文字filter
  typeFilter(type: any) {
    let tempObj: any = this.incomeExpenditureOptions.find((item: any) => {
      return item.content == type;
    })
    return tempObj?.name || '-';
  }
}
